﻿app.controller('CreateUserController', function ($scope, $rootScope, userFactory, notificationFactory) {
    init();

    function init() {
        $scope.severFile = mainFileAddress;
        $scope.roleByGroup = JSON.parse(localStorage.getItem("menu"));
        $scope.userName = $rootScope.userName;
        //count--------------
        notificationFactory.countAddFriend(localStorage.getItem("idkey")).success(function (data) {
            $scope.countAddFriend = data;
        });
        notificationFactory.countFromRoom(localStorage.getItem("idkey")).success(function (data) {
            $scope.countFromRoom = data;
        });
        notificationFactory.countNewMessage(localStorage.getItem("idkey")).success(function (data) {
            $scope.countNewMessage = data;
        });
        
        $scope.formData = { UserName: null, Password: null, PasswordC: null, FullName: null, Email: null, PhoneNumber: null, Block: true };
        $scope.checkEmail = true;
        $scope.checkEmailOnblur = function() {
            var emailstring = { SearchString: $scope.formData.Email };
            userFactory.checkEmail(emailstring).success(function(data) {
                if (data == 'true')
                    $scope.checkEmail = true;
                else {
                    $scope.checkEmail = false;
                }
            });
        };
        $scope.checkEmailFocus = function() {
            $scope.checkEmail = true;
        };


        $scope.checkUser = true;
        $scope.checkUserOnblur = function() {
            var userstring = { SearchString: $scope.formData.UserName };
            userFactory.checkUser(userstring).success(function(data) {
                if (data == 'true')
                    $scope.checkUser = true;
                else {
                    $scope.checkUser = false;
                }
            });
        };
        $scope.checkUserFocus = function() {
            $scope.checkUser = true;
        };
        $scope.createUser = function () {
            $scope.pageLoading = true;
            if ($scope.checkUser && $scope.checkEmail) {
                userFactory.postUser($scope.formData);
            }
            $("#createUserstatus").html("Tạo thành công!");
            $scope.pageLoading = false;
        };
    }
});

function createWord() {

}